﻿'AUTHOR:  ROBERT A BERNARD JR
'DATE:    01/15/2012
'SUBJ:    APPLICATION TO CREATE RANDOM PASSWORDS
'VERSION: 1.201

Public Class PasswordGeneratorForm

    'ADDING TO CODEPLEX
    'DECLARATION OF VARIABLES
    Dim Password As String = "" 'THIS IS THE PASSWORD HOLDER
    Dim Letter As String = "QWERTYUIPASDFGHJKLZXCBVBNM" 'THIS IS THE STRING OF AVAILABLE LETTERS
    Dim Numbers As String = "1234567890" 'THIS IS THE STRING OF AVAILABLE NUMBERS
    Dim Characters As String = Letter + Numbers 'THIS IS A CONBINATION OF THE AVAILABLE LETTERS AND NUMBERS FOR THE GENERATION OF THE PASSWORD
    Dim PassLength = 8 'THIS IS THE LENGTH OF THE PASSWORD STRING
    Dim RestOfCharLength As Integer = PassLength - 1 'THIS IS TO HELP CALCULATE THE LENGTH OF THE REST OF THE CHARACTERS
    Dim Random As New Random() 'THIS IS TO CALL THE RANDOMIZER AND CREATE A NEW METHOD FOR THE APPLICATION
    Dim FirstLetter As String 'THIS IS HOLDER FOR THE GENERATED FIRST CHARACTER OF THE PASSWORD
    Dim FirstLetterChar As String = Letter 'THIS IS TO COPY THE VALUE OF THE AVAILABLE LETTERS TO THIS HOLDER TO RANDOMIZE THE CHARACTERS
    Dim RestOfChar As String 'THIS IS THE HOLDER FOR THE REST OF THE PASSWORD CHARACTERS
    Dim ClearValue As String = "" 'THIS IS TO CLEAR VALUES ACROSS THE APPLICATION

    'THE WAY TO MAKE THIS WORK IT TO OPEN THE SOLUTION EXPLORER THEN RIGHT-CLICK ON THE PROJECT, WHICH IN THIS CASE IS PASSWORDGENERATOR.
    'THEN ADD THEN EXISTING ITEM. SELECT THE CHM FILE AND THEN CLICK ADD. ONCE THE FILE IS IN THE SOLUTION EXPLORER THEN RIGHT-CLICK.
    'SELECT PROPERTIES. IN THE PROPERTIES CHANGE BUILD ACTION TO "CONTENT" (TO GET INSTALL WITH THE REST OF THE FILES) 
    'AND COPY TO OUTPUT DIRECTORY TO "COPY IF NEWER" (TO GET COPY TO THE BUILT DIRECTORY)
    Dim path = System.IO.Path.Combine(Application.StartupPath, "PasswordGenerator.chm") 'PATH FOR THE CHM FILE

    'METHODS TO LOAD WHEN THE APPLICATION OPENS
    Private Sub PasswordGeneratorForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ClearButtonBehavior() 'CALL THE CLEAR BUTTON BEHAVIOR METHOD
        CopyButtonBehavior() 'THIS CALLS THE COPY BUTTON BEHAVIOR METHOD

    End Sub 'END METHODS TO LOAD WHEN THE APPLICATION OPENS

    'MENU EXIT
    Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click

        Close() 'CLOSES THE APPLICATION

    End Sub

    'BUTTON EXIT
    Private Sub ExitButton_Click(sender As System.Object, e As System.EventArgs) Handles ExitButton.Click

        Close() 'CLOSES THE APPLICATION

    End Sub

    'MENU CLEAR
    Private Sub ClearToolStripMenuItem1_Click(sender As System.Object, e As System.EventArgs) Handles ClearToolStripMenuItem1.Click

        ClearText() 'THIS IS PASSWORD BOX
        ClearButtonBehavior()

    End Sub

    'BUTTON CLEAR
    Private Sub ClearButton_Click(sender As System.Object, e As System.EventArgs) Handles ClearButton.Click

        ClearText() 'CLEAR THE PASSWORD BOX

    End Sub

    'BUTTON GENERATE
    Private Sub GenerateButton_Click(sender As System.Object, e As System.EventArgs) Handles GenerateButton.Click

        RandomPass() 'GENERATE THE PASSWORD

    End Sub

    'MENU GENERATE
    Private Sub GenerateToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles GenerateToolStripMenuItem.Click

        RandomPass() 'GENERATE THE PASSWORD

    End Sub

    'MENU ABOUT
    Private Sub AboutToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AboutToolStripMenuItem.Click

        AboutBox.Show() 'SHOW THE ABOUT WINDOW

    End Sub

    'MENU HELP
    Private Sub PasswordGeneratorHelpToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PasswordGeneratorHelpToolStripMenuItem.Click

        Help.ShowHelp(ParentForm, path, HelpNavigator.TableOfContents) 'DISPLAYS THE HELP FILE

    End Sub 'END HELP MENU




    'THIS IS THE LOGIC OF THE APPLICATION

    'THIS IS TO CLEAR THE TEXT
    Private Sub ClearText() 'START THE CLEAR TEXT METHIOD

        Try 'THIS IS TO CATCH ERRORS IN THE CLEAR TEXT METHOD

            If PasswordTextBox.Text = ClearValue Then 'THIS IS THE IF TO CATCH IF THE VALUE OF THE PASSWORD BOX IS EMPTY
                ToolStripStatusLabel.ForeColor = Color.Red 'SET THE STATUS BAR TEXT TO RED
                ToolStripStatusLabel.Text = "Password was already cleared" 'DISPLAYS MESSAGE IN THE STATUS BAR

            Else
                PasswordTextBox.Text = ClearValue 'THIS IS TO CLEAR THE PASSWORD BOX
                ToolStripStatusLabel.ForeColor = Color.Black 'SET THE STATUS BAR TEXT TO BLACK
                ToolStripStatusLabel.Text = "Password box cleared" 'DISPLAYS IN THE STATUS BAR
                ClearButtonBehavior()
                CopyButtonBehavior()

            End If 'END IF

        Catch ex As Exception 'IF AN ERROR HAPPENS THEN DISPLAY THIS MESSAGE
            ToolStripStatusLabel.ForeColor = Color.Red 'SET THE STATUS BAR TEXT TO RED
            ToolStripStatusLabel.Text = "Error on ClearText()" 'DISPLAYS IN THE STATUS BAR

        End Try 'END CATCH ERRORS

    End Sub 'END CLEARTEXT

    'THIS IS TO COPY THE TEXT TO THE CLIPBOARD
    Private Sub CopyText() 'START THE COPY TEXT METHOD

        Try 'THIS IS TO CATCH ERRORS IN THE COPY TEXT METHOD
            If PasswordTextBox.Text = ClearValue Then 'THIS IS TO CATCH IF THE PASSWORD BOX IS CLEARED
                ToolStripStatusLabel.ForeColor = Color.Red 'SET THE STATUS BAR TEXT TO RED
                ToolStripStatusLabel.Text = "Please Generate a Password First" 'DISPLAYS IN THE STATUS BAR

                'COMMENTING THIS CODE OUT WILL MAKE THE COPY BUTTON COPY INTO THE CLIPBOARD EVEN IF THE PASSWORD WAS ALREADY CPIED TO THE CLIPBOARD
                'ElseIf PasswordTextBox.Text = Clipboard.GetText Then 'THIS IS TO CATCH IF THE PASSWORD IS ALREADY COPIED TO THE CLIPBOARD
                'ToolStripStatusLabel.ForeColor = Color.Red 'SET THE STATUS BAR TEXT TO RED
                'ToolStripStatusLabel.Text = "Password already copied" 'DISPLAYS IN THE STATUS BAR

            Else
                Clipboard.SetText(PasswordTextBox.Text) 'COPY PASSWORD TO CLIPBOARD
                ToolStripStatusLabel.ForeColor = Color.Black 'SET THE STATUS BAR TEXT TO BLACK
                ToolStripStatusLabel.Text = "Password copied to clipboard" 'DISPLAYS IN THE STATUS BAR

            End If 'END IF

        Catch ex As Exception 'IF AN ERROR HAPPENS THEN DISPLAY THIS MESSAGE
            ToolStripStatusLabel.ForeColor = Color.Red 'SET THE STATUS BAR TEXT TO RED
            ToolStripStatusLabel.Text = "Error on CopyText()" 'DISPLAYS IN THE STATUS BAR

        End Try 'END CATCH ERRORS

    End Sub 'END THE COPY TEXT METHOD

    'THIS IS TO GENERATE A RANDOM PASSWORD
    Private Sub RandomPass() 'START THE RANDOMIZE PASSWORD METHOD

        Try 'THIS IS TO CATCH ERRORS IN THE RANDOMIZER METHOD
            'SET VALUES TO ZERO
            FirstLetter = ClearValue
            RestOfChar = ClearValue

            'FOR STATEMENT FOR THE FIRST LATTER
            For i = 1 To 1
                FirstLetter = FirstLetterChar.Substring(Random.Next(FirstLetterChar.Length), 1)
            Next

            'FOR STATEMENT FOR THE REST OF THE PASSWORD
            For i = 1 To RestOfCharLength
                RestOfChar += Characters.Substring(Random.Next(Characters.Length), 1)
            Next

            Password = FirstLetter.ToString + RestOfChar.ToString 'CONBINE VLUES INTO ONE AND SET EQUAL TO THE PASSWORD
            PasswordTextBox.Text = Password.ToString 'DISPLAY THE PASSWORD IN THE TEXT FIELD
            ToolStripStatusLabel.ForeColor = Color.Black
            ToolStripStatusLabel.Text = "Password generated and copied to clipboard"
            Clipboard.SetText(PasswordTextBox.Text) 'COPY PASSWORD TO CLIPBOARD
            ClearButtonBehavior()
            CopyButtonBehavior()

        Catch ex As Exception 'IF AN ERROR HAPPENS THEN DISPLAY THIS MESSAGE
            ToolStripStatusLabel.ForeColor = Color.Red 'SET THE STATUS BAR TEXT TO RED
            ToolStripStatusLabel.Text = "Error on RandomPass()" 'DISPLAYS IN THE STATUS BAR

        End Try 'END CATCH ERRORS

    End Sub 'ENDS THE RANDOMIZER PASSWORD METHOD

    'THIS IS THE METHOD FOR THE CLEAR BUTTON BEHAVIOR
    Private Sub ClearButtonBehavior()

        Try 'TRY THE FOLLOWING AND IF NOT DISPLAY ERROR

            If PasswordTextBox.Text = ClearValue Then 'IF THE PASSWORD BOX IS CLEAR THEN

                ClearButton.Enabled = False 'DISABLE THE CLEAR BUTTON
                ClearToolStripMenuItem1.Enabled = False 'DISABLE THE CLEAR MENU

            Else 'IF NOT THEN

                ClearButton.Enabled = True 'ENABLE THE CLEAR BUTTON
                ClearToolStripMenuItem1.Enabled = True 'ENABLE THE CLEAR MANU

            End If 'END IF

        Catch ex As Exception 'IF AN ERROR HAPPENS THEN DISPLAY THIS MESSAGE

            ToolStripStatusLabel.ForeColor = Color.Red 'SET THE STATUS BAR TEXT TO RED
            ToolStripStatusLabel.Text = "Error on ClearButtonBehavior()" 'DISPLAYS IN THE STATUS BAR

        End Try 'END CATCH ERRORS

    End Sub 'END THE CLEAR BUTTON BEHAVIOR

    'THIS IS THE BEHAVIOR OF THE COPY BUTTON
    Private Sub CopyButtonBehavior()

        Try

            If PasswordTextBox.Text = ClearValue Then
                CopyToolStripMenuItem.Enabled = False
                CopyButton.Enabled = False

                'COMMENTING THIS CODE OUT WILL MAKE THE COPY BUTTON VISIBLE TO COPY THE PASSWORD EVEN IF THE APPLICATION ALREADY COPIED IT INTO THE CLIPBOARD
                ' ElseIf Clipboard.GetText.ToString <> PasswordTextBox.Text Then
                'CopyToolStripMenuItem.Enabled = False
                'CopyButton.Enabled = False

            Else
                CopyToolStripMenuItem.Enabled = True
                CopyButton.Enabled = True

            End If
            
        Catch ex As Exception
            ToolStripStatusLabel.ForeColor = Color.Red 'SET THE STATUS BAR TEXT TO RED
            ToolStripStatusLabel.Text = "Error on CopyButtonBehavior()" 'DISPLAY IN THE STATUS BAR

        End Try

    End Sub

    'THIS ENABLES FUNCTIONS ON THE COPY BUTTON
    Private Sub CopyButton_Click(sender As Object, e As EventArgs) Handles CopyButton.Click

        Me.CopyText()

    End Sub

End Class
